package com.cn.springcloudwechat.common.utils.uploadMult;

import com.cn.springcloudwechat.common.utils.base.UuidUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.*;

public class UpLoadMultUtil {
	/**
	 * 加密文件名称
	 *@方法名:makeFileName
	 *@描述:
	 *@param fileName
	 *@return
	 */
	public static String makeFileName(String fileName) {
		return UuidUtil.get32UUID().trim().toString() + "_" + fileName;
	}
	/**
	 * Hash打散文件夹
	 *@方法名:makePath
	 *@描述:
	 *@param saveFileName
	 *@param savePath
	 *@return
	 */
	public static String makePath(String saveFileName, String savePath) {
		// 得到文件名的hashCode的值，得到的就是filename这个字符串对象在内存中的地址
		int hashcode = saveFileName.hashCode();
		int dir1 = hashcode & 0xf; // 0--15
		int dir2 = (hashcode & 0xf0) >> 4; // 0-15
		// 随机目录地址
		String randomDir = dir1 + "/" + dir2; // upload/2/3 // upload/3/5
		// 保存目录
		String dir = savePath + "/" + randomDir;
		// File既可以代表文件也可以代表目录
		File file = new File(dir);
		// 如果目录不存在
		if (!file.exists()) {
			// 创建目录
			file.mkdirs();
		}
		return randomDir;
	}
	/**
	 * 上传多个文件
	 *@方法名:UpLoadMult
	 *@描述:
	 *@param servicePath
	 *@param file
	 *@param request
	 *@return
	 */
	public static String[] UpLoadMult(String servicePath,MultipartFile[] file,HttpServletRequest request){
		//获取上传文件的保存目录
		String savePath = request.getSession().getServletContext().getRealPath("/") + servicePath;
		//服务器文件所存在的路径
		String realSavePath=null;
		String buff="";
		String p[] = null;
		for(int i=0;i<file.length;i++){
			String fileName = file[i].getOriginalFilename();
			if(!StringUtils.isEmpty(fileName)){
				System.err.println("fileName="+fileName);
				String format=fileName.substring(fileName.lastIndexOf(".")+1);
				if(format.equals("jpg")||format.equals("png")){
					//加密文件名
					String saveFileName = UpLoadMultUtil.makeFileName(fileName);
					//打散文件夹
					String randomDir=UpLoadMultUtil.makePath(saveFileName, savePath);
					//文件在服务器路径
					realSavePath= savePath + "/" + randomDir;
					buff+=servicePath+"/"+randomDir+"/"+saveFileName+"&";
					System.out.println("buff="+buff);
					// 创建一个文件输出流
					FileOutputStream out;
					try {
						out = new FileOutputStream(realSavePath + "/" + saveFileName);
						// 创建缓存区
						byte buffer[] = new byte[1024];
						int len = 0;
						InputStream inputStream = file[i].getInputStream();
						while ((len = inputStream.read(buffer)) > 0) {
							out.write(buffer, 0, len);
						}
						// 关闭输入流
						inputStream.close();
						// 关闭输出流
						out.close();
						p=buff.substring(0,buff.lastIndexOf("&")).split("&");
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
					System.out.println("图片格式不正确");
					break;
				}
			}else{
				System.out.println("图片为空");
				break;
			}
		}
		return p;
	}
	/**
	 * 上传单个文件
	 *@方法名:upLoadMult
	 *@描述:
	 *@param servicePath
	 *@param file
	 *@param request
	 *@return
	 */
	public static String upLoadMult(String servicePath,MultipartFile file,HttpServletRequest request){
		//String servicePath = "upload/picture";
		//获取上传文件的保存目录
		String savePath = request.getSession().getServletContext().getRealPath("/") + servicePath;
		//服务器文件所存在的路径
		String realSavePath=null;
		String buff="";
			String fileName = file.getOriginalFilename();
			if(!StringUtils.isEmpty(fileName)){
				System.err.println("fileName="+fileName);
				String format=fileName.substring(fileName.lastIndexOf(".")+1);
				if(format.equals("jpg")||format.equals("png")||format.equals("xlsx")||format.equals("xls")){
					//加密文件名
					String saveFileName = UpLoadMultUtil.makeFileName(fileName);
					//打散文件夹
					String randomDir=UpLoadMultUtil.makePath(saveFileName, savePath);
					//文件在服务器路径
					realSavePath= savePath + "/" + randomDir;
					buff=servicePath+"/"+randomDir+"/"+saveFileName;
					System.out.println("buff="+buff);
					// 创建一个文件输出流
					FileOutputStream out;
					try {
						out = new FileOutputStream(realSavePath + "/" + saveFileName);
						// 创建缓存区
						byte buffer[] = new byte[1024];
						int len = 0;
						InputStream inputStream = file.getInputStream();
						while ((len = inputStream.read(buffer)) > 0) {
							out.write(buffer, 0, len);
						}
						// 关闭输入流
						inputStream.close();
						// 关闭输出流
						out.close();
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
					System.out.println("文件格式不正确");
				}
			}else{
				System.out.println("文件为空");
			}
		return buff;
	}
	public static void main(String[] args) {
		String aString="aaa.jpg";
		String format=aString.substring(aString.lastIndexOf(".")+1);
		System.out.println(format);
	}
}
